<?php

declare(strict_types=1);

namespace app\shop\controller\admin\data;

use think\Request;
use think\Response;
use sheep\controller\Backend;
use app\shop\model\data\FakeUser as FakeUserModel;
use sheep\controller\traits\Crud;
use sheep\facade\Client;
use sheep\facade\Uploader;
use Intervention\Image\ImageManager;

class FakeUser extends Backend
{
    use Crud;

    protected $model = null;
    protected $isPaginate = true;
    protected $onlyParams = ['username', 'nickname', 'mobile', 'password', 'avatar', 'gender', 'email'];

    public function initialize()
    {
        $this->model = new FakeUserModel();
    }


    public function select(Request $request): Response
    {
        $list = $this->model->sheepFilter()->paginate($request->param('list_rows', 10));

        return success('获取成功', $list);
    }


    /**
     * 随机获取一个虚拟用户
     *
     * @return void
     */
    public function getRandom()
    {
        $userFake = $this->model->orderRaw('rand()')->find();

        return $userFake ? success('获取成功', $userFake) : error('请在数据维护中添加虚拟用户');
    }


    /**
     * 随机生成用户
     */
    public function random(Request $request)
    {
        set_time_limit(0);
        $num = $request->param('num', 1);

        for ($i = 0; $i < $num; $i++) {
            $fake = \Faker\Factory::create('zh_CN');

            $style = [
                'adventurer',
                'big-smile',
                'bottts',
                'croodles',
                'croodles-neutral',
                'identicon',
                'micah'
            ];

            $avatarSources = [
                // "https://joeschmoe.io/api/v1/random",        // 生成的是 svg ，无法使用
                "https://avatars.dicebear.com/api/%s/" . $fake->username() . ".png",
                // "https://api.multiavatar.com/" . $fake->username() . ".png"
            ];

            $avatarUrl = $avatarSources[array_rand($avatarSources)];
            $avatarUrl = sprintf($avatarUrl, $style[array_rand($style)]);
            $avatarResponse = Client::httpGet($avatarUrl);                                             // 获取二维码 数据流
            $imageManager = new ImageManager();
            $image = $imageManager->make($avatarResponse->getBody());
            $image->resize(200, 200);
            $path = app()->getRootPath() . 'runtime/storage/temp/';
            if (!is_dir($path)) {
                @mkdir($path, 0755, true);
            }
            $filePath = $path . md5(time() . mt_rand(1000, 9999)) . '.png';
            $image->save($filePath);

            $file = new \think\File($filePath);
            $result = \sheep\facade\Uploader::uploadSim($file);
            $avatar = $result['path'];
            @unlink($filePath);

            $fakeUser = new FakeUserModel();
            $fakeUser->username = $fake->username();
            $fakeUser->nickname = $fake->name;
            $fakeUser->mobile = $fake->phoneNumber;
            $fakeUser->password = $fake->password();
            $fakeUser->avatar = $avatar;
            $fakeUser->gender = mt_rand(0, 2);
            $fakeUser->email = $fake->email;
            $date = \Carbon\Carbon::now()->subDays(mt_rand(1, 30))->addMinutes(mt_rand(1, 1440))->addSeconds(mt_rand(1, 3600));
            $fakeUser->create_time = $date->timestamp;
            $fakeUser->update_time = $date->timestamp;
            $fakeUser->save();
        }

        return success('生成成功');
    }
}
